//
//  ViewController.m
//  SandH
//
//  Created by kuaiquan on 16/2/29.
//  Copyright © 2016年 chang. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self getPath];
    [self write];
    [self read];
}

- (void)getPath{
    //获取根目录
    NSString *homePath = NSHomeDirectory();
    NSLog(@"Home目录：%@",homePath);
    
    //获取Documents文件夹目录,第一个参数是说明获取Doucments文件夹目录，第二个参数说明是在当前应用沙盒中获取，所有应用沙盒目录组成一个数组结构的数据存放
    NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsPath = [docPath objectAtIndex:0];
    NSLog(@"Documents目录：%@",documentsPath);
    
    //获取Cache目录
    NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [cacPath objectAtIndex:0];
    NSLog(@"Cache目录：%@",cachePath);
    
    //Library目录
    NSArray *libsPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libPath = [libsPath objectAtIndex:0];
    NSLog(@"Library目录：%@",libPath);
    
    //temp目录
    NSString *tempPath = NSTemporaryDirectory();
    NSLog(@"temp目录：%@",tempPath);
}

- (void)write{
    //写入文件
    NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [docPath objectAtIndex:0];
    if (!documentsPath) {
        NSLog(@"目录未找到");
    } else {
        NSString *filePaht = [documentsPath stringByAppendingPathComponent:@"test.txt"];
        NSString *contents = @"I write 内容";
        [contents writeToFile:filePaht atomically:YES encoding:NSUTF8StringEncoding error:nil];
    }
}

- (void)read{
    //读取文件
    NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsPath = [docPath objectAtIndex:0];
    NSString *readPath = [documentsPath stringByAppendingPathComponent:@"test.txt"];
    NSString *fileContent = [[NSString alloc] initWithContentsOfFile:readPath encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"文件内容：%@",fileContent);
}

@end
